const util = require('../../../utils/util.js')
const app = getApp()
Page({
  data: {
    showTime:'',//影片上映日期
    params: { //url请求参数对象
      dateStr: '',
      movieId: 0,
      pageNum: 0,
      pageSize: 10,
      cityId: '0',
      city: '正在定位...',
      regionid: ''  // 区域
    },
    hasMore: true,
    isShow: false, //导航下拉框是否展开
    cityCinemaInfo: {}, //影院过滤菜单
    cinemas: [], //影院列表 
    nothing: false, //是否有符合过滤的影院
    noSchedule: false //当天是否有场次，原本时间是由后台返回的，但是缺少城市ID就没有返回，导致当天可能没有播放场次

  },
  onLoad(options) {
    this.initPage(options)
  },
  initPage(options){
    const movieId = options.movieId
    const areaName = options.areaName
    wx.setNavigationBarTitle({
      title: areaName
    })
    var that = this;
    this.setData({
      showTime: util.formatYear(new Date()), //影片上映日期
      params:{
        ...that.data.params,
        
        movieId, // 电影Id
      
      }
    })
    //select-time会触发事件来调用changeTime初始化数据
  },
  //获取影院列表
  search(e){
    const search = e.detail.value
    this.getCinemas({...this.data.params, search })
  },
  getCinemas(params) {
    const _this = this;
    wx.showLoading({
      title: '加载中...',
    })
    var pageIndex = _this.data.params.pageNum;
    const {latitude, longitude, cityId} = app.globalData.selectCity

    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.url + `/movie/getMovieCinemas`,
        method: 'GET',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          ...params,
          cityId,  //城市Id
          latitude,  // 维度
          longitude, //经度
          pageNum: ++pageIndex,
        },
        success(res) {
          wx.hideLoading()
          _this.setData({
            cinemas: res.data.rows,
            hasMore: false
          })
        }
      })
    })
  },
  //获取过滤菜单数据 -> 暂时为区域
  getFilter() {
    const _this = this;
    wx.request({
      url: app.globalData.url + '/movie/queryRegionList',
      method: 'GET',
      data: {
        cityId: app.globalData.selectCity.cityId
      },
      success: function (res) {
        _this.setData({
          cityCinemaInfo: {
            cinemas: [],
            district: [{regionid: "", regionname: "全城"}, ...res.data.data],
            params:{
              ..._this.params,
              regionid: ''
            }
          }
        })
      }
    })
  },
  //当选择的时间变化时触发
  changeTime(e){
    var that = this
    const day = util.formatYear(new Date(e.detail.day))
    this.setData({
      cinemas: [],
      isShow: false, //隐藏过滤下拉框
      noSchedule: false,
      hasMore: true,
      
      params: {
        ...that.data.params,
        dateStr: day,
        pageNum: 0
      }
    })
    // wx.showLoading({
    //   title: '正在加载...'
    // })
    this.getCinemas(this.data.params)
  },
  //当过滤条件变化时调用的函数
  changeCondition(e) {
    const {regionid} = e.detail.region
    wx.showLoading({
      title: '正在加载...'
    })
    this.setData({
      params: {
        ...this.data.params,
        regionid,
        pageNum: 0
      },
      cinemas: [],
      nothing: false
    }, () => {
      this.getCinemas(this.data.params).then((list) => {
        if (!list.length) {
          this.setData({
            nothing: true
          })
        }
        wx.hideLoading()
      })
    })
  },
  //导航下拉框状态变化时的处理，在下拉框展开时禁止滚动穿透
  toggleShow(e) {
    const item = e.detail.item
    this.setData({
      isShow: item !== -1
    })
  },
  onShow: function () {
    if (app.globalData.selectCity) {
      this.setData({
        city: app.globalData.selectCity.cityName
      })
    this.getFilter()
    this.getCinemas({...this.data.params, regionid: ''})
    }
  },
})